package test;

public class custome implements Runnable{
	 Thread t;
	 String name;
	 private srcs obj;
	 /**
	  * obj from main,so different thread can share the same obj.
	  * if obj=new Object() here in thread_sy class,each thread will create a new obj,so sychronize
	  * is invalid
	  */
	 
	 public custome(String name,srcs obj){
		 this.name=name;
		 this.obj=obj;
		 
	 }
	 public  void run(){
		 System.out.println("customer start"+obj.data);
		 /*while(true){
		 obj.data+=2;
		 try{
		 t.sleep(10);
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		 System.out.println("C:"+obj.data);
		 }*/
		 while(true){
		 synchronized (obj) {
			 while(obj.data==0){
				try{		
					System.out.println("consumer begin wait,obj is :"+obj.data);
					 obj.wait();
				   }catch( Exception e){
					 e.printStackTrace();
				   }
			     }
				obj.data--;
				System.out.println("consumer consume one,obj is :"+obj.data);
				obj.notify();
			 
		   }
		 }
			
	 }
	 public void start(){
		 if(t==null){
			 t=new Thread(this,this.name);
			 t.start();
		 }
	 }
}
